package org.myblog.myblogserver.code.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.myblog.myblogserver.code.entity.Message;
import org.myblog.myblogserver.code.entity.People;
import org.myblog.myblogserver.code.entity.User;
import org.myblog.myblogserver.code.service.MessageService;
import org.myblog.myblogserver.code.service.UserService;
import org.myblog.myblogserver.dao.MessageDao;
import org.myblog.myblogserver.dao.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "Message")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
    private Result result = new Result();


    /**
     * post  /message/insert<br/>
     * json <br/>
     * 以author_id为外键，判断用户是否存在<br/>
     *
     * @author 智哥
     * @param message
     * @return Result
     * */
    @ApiOperation(value = "Message新增")
    @PostMapping("/insert")
    public Result insert(@RequestBody Message message) {
        System.out.println("message = " + message);
        String author=message.getAuthor_id();
        if (userService.getById(author)==null){
            result.client_fail("用户不存在",null);
            return result;
        }
        message.setTime(new java.util.Date());
        if(messageService.save(message)){
            result.success("添加成功", null);
        }
        else{
            result.client_fail("添加失败", null);
        }
        return result;
    }


    /**
     * post  /message/selecteq<br/>
     * json <br/>
     * author_id和time 两个条件<br/>
     *
     * @author 智哥
     * @param message
     * @return Result
     * */
    @ApiOperation(value = "Message条件查询")
    @PostMapping("/selecteq")
    public Result selecteq(@RequestBody Message message) {
        System.out.println("message = " + message);
        QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
        if(message.getAuthor_id()!=null) queryWrapper.like("author_id",message.getAuthor_id());
        if(message.getTime()!=null) queryWrapper.like("time",message.getTime());
        List<Message> list = messageService.list(queryWrapper);
        if(list.size()>0){
            List<MessageDao> li=new ArrayList<>();
            for(Message me:list){
                MessageDao messageDao = new MessageDao();
                messageDao.setId(me.getId());
                messageDao.setUsername(userService.getById(me.getAuthor_id()).getUsername());
                messageDao.setComment(me.getComment());
                messageDao.setTime(me.getTime());
                messageDao.setSupport(me.getSupport());
                li.add(messageDao);
            }
            result.success("查询成功", li);
        }
        else{
            result.client_fail("查询失败", null);
        }
        return result;
    }

    /**
     * post  /message/update<br/>
     * /message/update/?id=1 <br/>
     *
     * @author 智哥
     * @param id
     * @return Result
     * */
    @ApiOperation(value = "Message修改")
    @PostMapping("/update")
    public Result update(@RequestParam(value = "id") Integer id) {
        Message m=messageService.getById(id);
        m.setSupport(m.getSupport()+1);
        if(messageService.updateById(m)){
            result.success("修改成功", null);
        }else {
            result.server_fail("修改失败", null);
        }
        return result;
    }


    /**
     * post  /message/deleteAlleq<br/>
     * json <br/>
     * author_id和time和Comment和id 四个条件<br/>
     *
     * @author 智哥
     * @param message
     * @return Result
     * */
    @ApiOperation(value = "Message条件删除")
    @PostMapping("/deleteAlleq")
    public Result deleteAlleq(@RequestBody Message message) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if(message.getAuthor_id()!=null) queryWrapper.eq("author_id",message.getAuthor_id());
        if(message.getTime()!=null) queryWrapper.eq("time",message.getTime());
        if(message.getComment()!=null) queryWrapper.eq("comment",message.getComment());
        if(message.getId()!=null) queryWrapper.eq("id",message.getId());
        if(messageService.remove(queryWrapper)){
            result.success("删除成功", null);
        }
        else{
            result.client_fail("删除失败", null);
        }
        return result;
    }


    /**
     * get  /message/selectCount<br/>
     * Message记录数查询<br/>
     *
     * @author 智哥
     * @return Result
     * */
    @GetMapping("/selectCount")
    @ApiOperation(value = "User记录数查询")
    public Result selectCount() {
        long a=messageService.count();
        System.out.println("a = " + a);
        if(a>=0){
            result.success("查询成功",a);
        }
        else {
            result.server_fail("查询失败",0);
        }
        return result;
    }

}